/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.service.VOrderCardShopService;
import com.ykht.proj.activity.vo.CardShopDto;
import com.ykht.proj.activity.vo.VConsumeRecordVo;
import com.ykht.proj.admin.api.entity.SysUser;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.activity.service.VConsumeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 核销表（消费记录表）
 *
 * @author csg
 * @date 2021-05-21 14:21:47
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vconsumerecord" )
@Api(value = "vconsumerecord", tags = "核销表（消费记录表）管理")
@Slf4j
public class VConsumeRecordController {

	private final RemoteUserService remoteUserService;
    private final  VConsumeRecordService vConsumeRecordService;
    private final VOrderCardShopService vOrderCardShopService;

    /**
     * 卡核销记录 分页查询
     * @param page 分页对象
     * @param vConsumeRecordVo 核销表（消费记录表）
     * @return
     */
    @ApiOperation(value = "卡核销记录 分页查询", notes = "卡核销记录 分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('activity_vconsumerecord_view')" )
    public R getVConsumeRecordPage(Page page, VConsumeRecordVo vConsumeRecordVo) {
		log.info("->卡核销记录 分页查询{}{}", page, vConsumeRecordVo);
		IPage<VConsumeRecordVo> iPage = vConsumeRecordService.getVConsumeRecordPage(page, vConsumeRecordVo);
		if (null != iPage.getRecords() && iPage.getRecords().size() > 0){
			List<VConsumeRecordVo> recordVoList = iPage.getRecords();
			List<Long> longList = recordVoList.stream().map(VConsumeRecordVo::getLoginId).collect(Collectors.toList());
			List<SysUser> userList = remoteUserService.userByIds(longList);
			recordVoList.stream().forEach(itme -> {
				userList.stream().forEach(user -> {
					if (itme.getLoginId().equals(Long.valueOf(user.getUserId()))) {
						itme.setUsername(user.getUsername());
						itme.setNickName(user.getNickName());
						itme.setUserPhone(user.getPhone());
					}
				});
			});
			iPage.setRecords(recordVoList);
		}
        return R.ok(iPage);
    }


	/**
	 * 根据卡获取核销记录列表 分页查询
	 * @param page 分页对象
	 * @param vConsumeRecordVo 核销表（消费记录表）
	 * @return
	 */
	@ApiOperation(value = "根据卡获取核销记录列表 分页查询", notes = "根据卡获取核销记录列表 分页查询")
	@GetMapping("/getByCardConsumeRecordPage" )
	public R getByCardConsumeRecordPage(Page page, VConsumeRecordVo vConsumeRecordVo) {
		log.info("->根据卡获取核销记录列表 分页查询{}{}", page, vConsumeRecordVo);
		QueryWrapper<VConsumeRecord> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(VConsumeRecord::getInfoId, vConsumeRecordVo.getInfoId())
				.like(!StringUtils.isEmpty(vConsumeRecordVo.getShopName()), VConsumeRecord::getShopName, vConsumeRecordVo.getShopName())
				.like(!StringUtils.isEmpty(vConsumeRecordVo.getBrandName()), VConsumeRecord::getBrandName, vConsumeRecordVo.getBrandName())
				.gt(null != vConsumeRecordVo.getCreateTimeStart(), VConsumeRecord::getCreateTime, vConsumeRecordVo.getCreateTimeStart())
				.lt(null != vConsumeRecordVo.getCreateTimeEnd(), VConsumeRecord::getCreateTime, vConsumeRecordVo.getCreateTimeEnd());
		return R.ok(vConsumeRecordService.page(page, queryWrapper));
	}



	/**
     * 通过id查询核销表（消费记录表）
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('activity_vconsumerecord_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(vConsumeRecordService.getById(id));
    }

    /**
     * 新增核销表（消费记录表）
     * @param vConsumeRecord 核销表（消费记录表）
     * @return R
     */
    @ApiOperation(value = "新增核销表（消费记录表）", notes = "新增核销表（消费记录表）")
    @SysLog("新增核销表（消费记录表）" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('activity_vconsumerecord_add')" )
    public R save(@RequestBody VConsumeRecord vConsumeRecord) {
        return R.ok(vConsumeRecordService.save(vConsumeRecord));
    }

    /**
     * 修改核销表（消费记录表）
     * @param vConsumeRecord 核销表（消费记录表）
     * @return R
     */
    @ApiOperation(value = "修改核销表（消费记录表）", notes = "修改核销表（消费记录表）")
    @SysLog("修改核销表（消费记录表）" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('activity_vconsumerecord_edit')" )
    public R updateById(@RequestBody VConsumeRecord vConsumeRecord) {
        return R.ok(vConsumeRecordService.updateById(vConsumeRecord));
    }

    /**
     * 通过id删除核销表（消费记录表）
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除核销表（消费记录表）", notes = "通过id删除核销表（消费记录表）")
    @SysLog("通过id删除核销表（消费记录表）" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('activity_vconsumerecord_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(vConsumeRecordService.removeById(id));
    }


	/**
	 * 根据会员卡ID获取 卡还能使用的门店数量
	 * @param infoId      会员卡ID
	 * @param planType    方案类型  0企业优惠  1活动优惠  (不传为全部)
	 * @return R
	 */
	@ApiOperation(value = "根据会员卡ID获取 卡还能使用的门店数量", notes = "根据会员卡ID获取 卡还能使用的门店数量")
	@GetMapping("/getByInfoIdCountShop" )
	public R getByInfoIdCountShop(Long infoId, Integer planType) {
		log.info("->根据会员卡ID获取 卡还能使用的门店数量{}", infoId, planType);
		return vOrderCardShopService.getByInfoIdCountShop(infoId, planType);
	}

}
